<?php 
	session_start();
	include('utils.php');
	
	if (!isset($_POST['per-day']) && !isset($_SESSION['initial-smoke-per-day'])) {
		//first-use questionnaire hasn't been filled out yet - redirect to it
		header('Location: index-first-use.php');
	} else {
		if (isset($_POST['per-day']) || !isset($_SESSION['initial-smoke-per-day'])) $_SESSION['initial-smoke-per-day'] = $_POST['per-day'];
		if (isset($_POST['per-week']) || !isset($_SESSION['spend-per-week']))  $_SESSION['spend-per-week'] = $_POST['per-week'];
		if (isset($_POST['savings-goal-input']) || !isset($_SESSION['savings-goal'])) $_SESSION['savings-goal'] = $_POST['savings-goal-input'];
		if (!isset($_SESSION['current-smoke-per-day'])) $_SESSION['current-smoke-per-day'] = $_SESSION['initial-smoke-per-day'];
		if (!isset($_SESSION['days-active'])) $_SESSION['days-active'] = 0;
	}
?>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>iQuit | Home</title>
        <meta name="description" content="A mobile application to help you quit smoking.">
        <meta name="viewport" content="width=device-width">
        <link rel="stylesheet" href="css/style.css">
		<link href='http://fonts.googleapis.com/css?family=Muli' rel='stylesheet' type='text/css'>
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
		<script src="js/jquery.nicescroll.js"></script>
		<script>
			function init() {
				$("#scroller").niceScroll({autohidemode:false});
				
				function update() {
					document.getElementById("update").style.display = "block";
					document.getElementById("update-smokes").style.display = "none";
				}
				
				if (<?php echo $_SESSION['days-active']; ?> == 30) document.getElementById("skip-btn").innerHTML = "Switch to 0 days active";
				else document.getElementById("skip-btn").innerHTML = "Switch to 30 days active";
				
				document.getElementById("update-smokes").addEventListener('click', update, false);
			}
			
			window.addEventListener('load', init, false);
			
		</script>
	</head>
	<body>
		<div id="skip-forward">
			<form id="skip-form" action="skip-forward.php" method="post">
				<button type="submit" id="skip-btn" name="skip-btn"></button>
			</form>
		</div>
		<div id="main-wrapper">
			<?php 
				include "includes/nav.php";
			?>
			<header>
				iQuit
			</header>
			<div id="scroller">
				<div id="content-index">
					<?php 
						if ($_SESSION['days-active'] > 0) echo "<p>You've been active for ".$_SESSION['days-active']." days.</p>";
						else echo "<p>You've just started your journey!  Good luck!</p>";
					?>
					<h3>
						Latest Milestones
					</h3>
					<div id="milestones" style="padding-left: 20px;"><br/>
						<?php				
							if ($_SESSION['days-active'] == 0) echo "<p>You'll reach your first milestone in just 12 hours!</p>";
							else if ($_SESSION['days-active'] < 3 ) echo "<span><strong>12 hours</strong></span><p>The carbon monoxide level in your blood has returned to normal and oxygen levels are increasing. Almost all the nicotine will be gone from your body!</p>";
							else if ($_SESSION['days-active'] < 30) echo "<span><strong>3 Days</strong></span><p>Your breath, clothes and hair is smelling fresher, your meals taste better and nicotine stains on your fingers are disappearing.</p>";
							else if ($_SESSION['days-active'] < 60) echo "<span><strong>One Month</strong></span><p>Your blood pressure is back to a normal level</p>";
							else if ($_SESSION['days-active'] < 270) echo "<span><strong>Two Months</strong></span><p>Your blood circulation has started to improve. Your risk of heart attack has started dropping and your lung function has started to improve!</p>";
							else if ($_SESSION['days-active'] < 365) echo "<span><strong>Nine Months</strong></span><p>Breathlessness and coughing has decreased.</p>";
							else echo "<p><strong>One Year</strong></p><p>Congratulations on reaching one year! Your risk of dying from heart disease is now half that of a continuing smoker.</p>";
							if ($_SESSION['current-smoke-per-day'] < $_SESSION['initial-smoke-per-day']) echo "<p>You're down to ".$_SESSION['current-smoke-per-day']." cigarettes per day!</p>"; 
							$savings = round(calc_savings($_SESSION['spend-per-week'], $_SESSION['initial-smoke-per-day'], $_SESSION['current-smoke-per-day'], $_SESSION['days-active']), 2);
							if ($savings > 0) echo "<p>You've saved a total of $".round(calc_savings($_SESSION['spend-per-week'], $_SESSION['initial-smoke-per-day'], $_SESSION['current-smoke-per-day'], $_SESSION['days-active']), 2)."</p>";
						?>
					</div>
					<h3>
						Current goal
					</h3>
					<p>
						<?php 
							if ($_SESSION['current-smoke-per-day'] > 0) echo "Work on getting down to ".($_SESSION['current-smoke-per-day'] - 1)." per day."		
						?>
					</p>
					<button type="button" id="update-smokes" name="update-smokes">Progress Update</button>
					<div class="pop-up-overlay" id="update" style="display: none;">
						<div class="text">
							<form name="index-goal" action="indexsubmit.php" method="post">
								How many cigarettes do you currently smoke per day? 
								<input type="text" name="cur-per-day" id="cur-per-day" maxlength="3" size="3" />
								<input class="btn" id="ok-btn" type="submit" value="OK" />
							</form>
						</div>						
					</div>
				</div>
			</div>
		</div>
	</body>
</html>